package com.ghost.alibabaghost.web.controller.sys;

import com.ghost.alibabaghost.api.model.sys.SysUserRoleModel;
import com.ghost.alibabaghost.api.service.sys.SysUserRoleService;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/14 23:30
 */

@RestController
@RequestMapping("userRole")
public class SysUserRoleController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserRoleController.class);

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 根据userid获取角色
     *
     * @param userId
     * @return
     */
    @RequestMapping("getUserRoleByUserId")
    public SingleResult<SysUserRoleModel> getUserRoleByUserId(@RequestParam("userId") Integer userId) {
        logger.info("getUserRoleByUserId（根据userid获取角色）--->param:userId=" + userId);
        try {
            SysUserRoleModel sysUserRoleModels = sysUserRoleService.queryRoleByUserId(userId);
            return SingleResult.buildSuccess(sysUserRoleModels);
        } catch (Exception e) {
            logger.error("getUserRoleByUserId（根据userid获取角色）--->", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 新增
     *
     * @param sysUserRoleModel
     * @return
     */
    @RequestMapping("insertUserRole")
    public SingleResult<String> insertUserRole(@RequestBody SysUserRoleModel sysUserRoleModel) {
        logger.info("insertUserRole（根据userid获取角色）--->param:" + sysUserRoleModel);
        try {
            sysUserRoleService.insertUserRole(sysUserRoleModel);
            return SingleResult.buildSuccess("成功");
        } catch (Exception e) {
            logger.error("insertUserRole（根据userid获取角色）--->", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 更新
     *
     * @param sysUserRoleModel
     * @return
     */
    @RequestMapping("updateByUserRole")
    public SingleResult<String> updateByUserId(@RequestBody SysUserRoleModel sysUserRoleModel) {
        logger.info("updateByUserId（更新）--->param:" + sysUserRoleModel);
        try {
            sysUserRoleService.updateByUserId(sysUserRoleModel);
            return SingleResult.buildSuccess("成功");
        } catch (Exception e) {
            logger.error("updateByUserId（更新）--->", e);
            return SingleResult.buildFailure();
        }
    }


}
